<template>
  <div class="container">
    <div class="el-toolbar">
      <div class="el-toolbar-body" style="justify-content: space-around;margin: 15px;">
        <el-button type="danger" size="small" @click="exportData">导出字典</el-button>
        <el-button type="primary" size="small" @click="importData">导入字典</el-button>
      </div>
    </div>
    <div class="form">
      <el-table :data="dictList" style="width: 100%" 
      row-key="id" border lazy :load="getChildrens"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <template v-for="item in columns">
          <el-table-column v-bind="item"></el-table-column>
        </template>
      </el-table>
    </div>
    <div class="dialog">
      <el-dialog title="导入字典" :visible.sync="dialogImportVisible" width="480px">
        <el-form label-position="right" label-width="50px">
          <el-form-item label="文件">
            <el-upload :multiple="false"
            drag
            :on-success="onUploadSuccess"
            :action="`/cmn-api/admin/cmn/dict/importData`"
            class=" upload-demo"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div slot="tip" class="el-upload__tip">只能上传xls文件, 且不超过500kb</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="danger">取消</el-button>
          <el-button type="primary">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import dict from '@/api/dict';

export default {
  name: 'DictLsit',
  data() {
    return {
      dictList: [],
      columns: [
        {
          label: '名称',
          width: 230,
          prop: 'name'
        },
        {
          label: '编码',
          width: 220,
          align: 'center',
          prop: 'dictCode'
        },
        {
          label: '值',
          width: 230,
          align: 'center',
          prop: 'value'
        },
        {
          label: '创建时间',
          align: 'center',
          prop: 'createTime'
        },
      ],
      dialogImportVisible: false 
    }
  },
  created() {
    this.getDictList(1) 
  },
  methods: {
    importData() {
      this.dialogImportVisible = true
    },
    onUploadSuccess() {
      this.dialogImportVisible = false
      this.getDictList(1)
    },
    exportData() {
      window.location.href = "/cmn-api/admin/cmn/dict/exportData"
    },
    getDictList(id) {
      dict.getdictList(id).then((res) => {
        this.dictList = res.data
        console.log(this.dictList);
      }).catch((error) => {
        console.log(error);
      })
    },
    getChildrens(tree, treeNode, resolve) {
      dict.getdictList(tree.id).then((res) => {
        resolve(res.data)
      })
    }
  }
}
</script>

<style scoped>
.el-form-item{
  margin-bottom: 0 !important;
}
</style>
